unit uLogin;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.Controls.Presentation, FMX.Edit, FMX.StdCtrls, FMX.Layouts,
  FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf,
  FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Stan.Async,
  FireDAC.Phys, FireDAC.Phys.IB, FireDAC.Phys.IBDef, FireDAC.Stan.Param,
  FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, System.Rtti,
  System.Bindings.Outputs, Fmx.Bind.Editors, Data.Bind.EngExt,
  Fmx.Bind.DBEngExt, Data.Bind.Components, Data.Bind.DBScope, Data.DB,
  FireDAC.Comp.DataSet, FireDAC.Comp.Client, FMX.ListView.Types, FMX.ListView,
  FireDAC.FMXUI.Wait, FireDAC.Phys.IBBase, FireDAC.Comp.UI;

type
  TfrmLogin = class(TForm)
    Layout1: TLayout;
    Layout3: TLayout;
    Layout4: TLayout;
    Layout2: TLayout;
    Label2: TLabel;
    Layout5: TLayout;
    edPassword: TEdit;
    Layout7: TLayout;
    edEmail: TEdit;
    Layout6: TLayout;
    Label3: TLabel;
    Layout8: TLayout;
    Button1: TButton;
    Layout9: TLayout;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmLogin: TfrmLogin;
  QResult: String;

implementation

{$R *.fmx}

uses uMain, uModule;


procedure TfrmLogin.Button1Click(Sender: TObject);
begin
  if (edEmail.Text <> '') and (edPassword.Text <> '') then
  Begin
    QResult := DataModule1.AceidbConnection.ExecSQLScalar('Select email from users where email = :email', [UpperCase(edEmail.Text)]);
    if QResult = '' then
    Begin
      If DataModule1.AceidbConnection.ExecSQL('insert into users values (:email, :password, :connected)', [UpperCase(edEmail.Text), UpperCase(edPassword.Text), true]) = 0 then
      begin
        ShowMessage('Error - The registration was unsuccessful, please, try again');
      end
      Else
      Begin
        ShowMessage('Success - User has been created');
        UserConnected := UpperCase(edEmail.Text);
        frmMain := TfrmMain.Create(nil);
        frmMain.Show;
      End;
    End
    else
    Begin
      ShowMessage('Warning - Email already registered');
    End;
  End
  Else
  Begin
    ShowMessage('Warning - Fill all the fields');
  End;
end;

procedure TfrmLogin.Button2Click(Sender: TObject);
begin
  if (edEmail.Text <> '') and (edPassword.Text <> '') then
  Begin
    QResult := DataModule1.AceidbConnection.ExecSQLScalar('Select email from users where email = :email and passwd = :password', [UpperCase(edEmail.Text), UpperCase(edPassword.Text)]);
    if QResult = '' then
    Begin
      ShowMessage('Warning - Invalid username or password');
    End
    Else
    Begin
      ShowMessage('Success - User connected');
      UserConnected := UpperCase(edEmail.Text);
      frmMain := TfrmMain.Create(nil);
      frmMain.Show;
    End;
  End
  Else
  Begin
    ShowMessage('Warning - Fill all the fields');
  End;
end;

end.
